public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    public MySingleList(){

    }
    public MySingleList(ListNode head) {
        this.head = head;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含关键字key
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int val){
        ListNode listNode = new ListNode(val);
        listNode.next = this.head;
        this.head = listNode;
    }
    //尾插法
    public void addLast(int val){
        ListNode listNode = new ListNode(val);
        ListNode cur = this.head;
        if(cur == null){
            this.head = listNode;
            return;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = listNode;
    }

    //在任意位置插入
    public void addIndex(int pos, int val){
        //先判断位置是否合法
        if(pos < 0 || pos > this.size()){
            throw new PosIlegalException(pos + " 位置非法");
        }
        if(pos == 0){
            addFirst(val);
            return;
        }
        ListNode cur = findNodeBeforeIndex(pos);
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findNodeBeforeIndex(int index){
        ListNode cur = this.head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public void delNode(int index){
        if(index < 0 || index > this.size()){
            throw new PosIlegalException(index + " 位置不合法");
        }
        if(index == 0){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findNodeBeforeIndex(index);
        cur.next = (cur.next).next;
    }

    //删除节点
    public void remove(int key){
        if(this.head == null){
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findNodeBeforeKey(key);
        if(cur == null){
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findNodeBeforeKey(int key){
        ListNode cur = this.head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
        }
        return null;
    }
}
